package com.lovo.project.service.impl;

import com.lovo.project.dao.ProductDAO;
import com.lovo.project.dao.RolePermissionDAO;
import com.lovo.project.enums.ProductEnum;
import com.lovo.project.enums.RolePermissionEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.PermissionDO;
import com.lovo.project.service.RolePermissionService;
import com.lovo.project.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @title  角色权限业务实现类
 * @author: chenqiang
 * @date: 2020/11/4 15:51
 */
public class RolePermissionServiceImpl implements RolePermissionService {
    @Override
    public List<PermissionDO> findPermissionByRoleId(Integer roleId) {
        SqlSession sqlSession  = MyBatisUtil.getSqlSession();
        RolePermissionDAO dao = sqlSession.getMapper(RolePermissionDAO.class);
        List<PermissionDO> list =  dao.findPermissionByRoleId(roleId);

        try {
            if(list.size()<=0){
                throw new OurException(RolePermissionEnum.PRODUCT_Find_ENUM);
            }
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
        return list;
    }

    @Override
    public List<PermissionDO> findOtherPermissionByRoleId(Integer roleId) {
        SqlSession sqlSession  = MyBatisUtil.getSqlSession();
        RolePermissionDAO dao = sqlSession.getMapper(RolePermissionDAO.class);
        List<PermissionDO> list =  dao.findOtherPermissionByRoleId(roleId);

        try {
            if(list.size()<=0){
                throw new OurException(RolePermissionEnum.PRODUCT_Find_ENUM);
            }
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
        return list;
    }

    @Override
    public void deletePermissionByRoleIdAndPermissionIdList(Integer roleId) {
        SqlSession sqlSession  = MyBatisUtil.getSqlSession();
        RolePermissionDAO dao = sqlSession.getMapper(RolePermissionDAO.class);
        int num= dao.deletePermissionByRoleIdAndPermissionIdList(roleId);

        try {
            if(num<=0){
                throw new OurException(RolePermissionEnum.PRODUCT_DELETE_ENUM);
            }
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }

    }

    @Override
    public void addPermissionByRoleIdAndPermissionIdList(Integer roleId, List<String> permissionIdList) {
        SqlSession sqlSession  = MyBatisUtil.getSqlSession();
        RolePermissionDAO dao = sqlSession.getMapper(RolePermissionDAO.class);
        int num= dao.addPermissionByRoleIdAndPermissionIdList(roleId,permissionIdList);

        try {
            if(num<=0){
                throw new OurException(RolePermissionEnum.PRODUCT_ADD_ENUM);
            }
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }
}
